package com.ecube.maintenance.biz.apis.impl;

import com.ecube.maintenance.biz.apis.IOrderAction;
import com.ecube.maintenance.biz.commons.Constants;
import com.ecube.maintenance.biz.commons.ICCallBack;
import com.ecube.maintenance.biz.commons.ITestDataProvider;
import com.ecube.maintenance.biz.commons.VolleySingleton;
import com.ecube.maintenance.pojos.BaseInfo;
import com.ecube.maintenance.pojos.OrderInfo;
import com.ecube.maintenance.pojos.ProductInfo;
import com.ecube.maintenance.pojos.SellerInfo;
import com.ecube.maintenance.pojos.UserInfo;
import com.ecube.maintenance.utils.BaseParameters;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * Created by Trible Chen on 2014/9/8.
 */
public class OrderAction {

    public static void submit(UserInfo info, SellerInfo sellerInfo, ProductInfo productInfo,
                              String arriveTime,
                              ICCallBack<OrderInfo> callBack){
        proxy.submit(info, sellerInfo, productInfo, arriveTime, callBack);
    }

    public static void fetchOrderList(String sid, String dataType,
                                      ICCallBack<List<OrderInfo>> callBack){
        proxy.fetchOrderList(sid, dataType, callBack);
    }

    public static void fetchOrderDetails(String sid, String orderNO,
                                         ICCallBack<OrderInfo> callBack){
        proxy.fetchOrderDetails(sid, orderNO, callBack);
    }

    public static void cancelOrder(String sid, String orderNO,
                                   ICCallBack<OrderInfo> callBack){
        proxy.cancelOrder(sid, orderNO, callBack);
    }

    private static ProxyOrderAction proxy = new ProxyOrderAction();

    private OrderAction() {
    }

    private static class ProxyOrderAction implements IOrderAction{
        @Override
        public void submit(UserInfo info,
                           SellerInfo sellerInfo, ProductInfo productInfo,
                           String arriveTime,
                           ICCallBack<OrderInfo> callBack) {
            if ( info == null
                    || sellerInfo == null
                    || productInfo == null ){
                throw new IllegalArgumentException("infos cant be null");
            }
            BaseParameters parameters = new BaseParameters();
            parameters.add("action","app_submit_order");
            parameters.add("spID",sellerInfo.getSpID());
            parameters.add("sid",info.getSid());
            parameters.add("productID",productInfo.getProductID());
            parameters.add("serviceStartTime",productInfo.getServiceStartTime());
            parameters.add("serviceEndTime",productInfo.getServiceEndTime());
            parameters.add("serviceDate",productInfo.getServiceDate());
            parameters.add("arriveTime",arriveTime);
            parameters.add("realName",info.getRealName());
            parameters.add("carBrand",info.getCarBrand());
            parameters.add("mobile",info.getMobile());
            parameters.add("carFactory",info.getCarFactory());
            parameters.add("carSerial",info.getCarSerial());
            parameters.add("carModel",info.getCarModel());
            parameters.add("carNO",info.getCarNO());
            parameters.add("driveMile",info.getDriveMile());
            UserInfo.saveToSpf(info);
            TypeToken<BaseInfo<OrderInfo>> typeToken =
                     new TypeToken<BaseInfo<OrderInfo>>(){};
            VolleySingleton.getInstance().addToRequestQueue(
                    parameters,callBack,typeToken.getType());

        }

        @Override
        public void fetchOrderList(String sid, String dataType,
                                   ICCallBack<List<OrderInfo>> callBack) {
            BaseParameters parameters = new BaseParameters();
            parameters.add("action","app_GetOrder");
            parameters.add("sid",sid);
            parameters.add("dataType",dataType);
            ITestDataProvider provider = null;
            if (Constants.LOCAL_TEST_DATA){
                provider = new ITestDataProvider() {
                    @Override
                    public String getTestData() {
                        return "{\n" +
                                "\"errorCode\": 10000,\n" +
                                "\"errorMsg\": \"\",\n" +
                                "\"returnData\": [\n" +
                                "{\n" +
                                "\"orderNO\": \"订单号\",\n" +
                                "\"createTime\": \"订单时间\",\n" +
                                "\"orderStatus\": \"订单状态\",\n" +
                                "\"productID\": \"产品 ID\",\n" +
                                "\"productName\": \"产品名称\",\n" +
                                "\"productPrice\": \"产品总额（特价价格）\",\n" +
                                "\"spID\": \"服务商 ID\",\n" +
                                "\"spName\": \"服务商名称\", \n" +
                                "\"spBrand\": \"服务商品牌描述\"\n" +
                                "},\n" +
                                "{\n" +
                                "\"orderNO\": \"订单号\",\n" +
                                "\"createTime\": \"订单时间\",\n" +
                                "\"orderStatus\": \"订单状态\",\n" +
                                "\"productID\": \"产品 ID\",\n" +
                                "\"productName\": \"产品名称\",\n" +
                                "\"productPrice\": \"产品总额（特价价格）\",\n" +
                                "\"spID\": \"服务商 ID\",\n" +
                                "\"spName\": \"服务商名称\",\n" +
                                "\"spBrand\": \"服务商品牌描述\"\n" +
                                "}\n" +
                                "]\n" +
                                "}";
                    }
                };
            }
            TypeToken<BaseInfo<List<OrderInfo>>> typeToken =
                    new TypeToken<BaseInfo<List<OrderInfo>>>(){};
            VolleySingleton.getInstance().addToRequestQueue(
                    parameters,callBack,typeToken.getType(),provider);
        }

        @Override
        public void fetchOrderDetails(String sid, String orderNO,
                                      ICCallBack<OrderInfo> callBack) {
            BaseParameters parameters = new BaseParameters();
            parameters.add("action","app_GetOrderDetail");
            parameters.add("sid",sid);
            parameters.add("orderNO",orderNO);
            ITestDataProvider provider = null;
            if (Constants.LOCAL_TEST_DATA){
                provider = new ITestDataProvider() {
                    @Override
                    public String getTestData() {
                        return "{\n" +
                                "\"errorCode\": 10000,\n" +
                                "\"errorMsg\": \"\", \n" +
                                "\"returnData\": {\n" +
                                "\"orderNO\": \"订单号\",\n" +
                                "\"createTime\": \"订单创建时间\",\n" +
                                "\"orderStatus\": \"订单状态\",\n" +
                                "\"orderTimeline\": [\n" +
                                "{\n" +
                                "\"orderAction\": \"跟踪内容 1\",\n" +
                                "\"actionTime\": \"发生时间 1\"\n" +
                                "},\n" +
                                "{\n" +
                                "\"orderAction\": \"跟踪内容 2\",\n" +
                                "\"actionTime\": \"发生时间 2\"\n" +
                                "},\n" +
                                "{\n" +
                                "\"orderAction\": \"跟踪内容 n\",\n" +
                                "\"actionTime\": \"发生时间 n\"\n" +
                                "}\n" +
                                "],\n" +
                                "\"productInfo\": {\n" +
                                "\"productID\": \"产品 ID\",\n" +
                                "\"productName\": \"产品名称\",\n" +
                                "\"productImg\": \"产品图片 url\",\n" +
                                "\"serviceStartTime\" : \"产品开始时间\",\n" +
                                "\"serviceEndTime\" : \"产品截止时间\",\n" +
                                "\"origPrice\": \"原价\",\n" +
                                "\"discountPrice\": \"折扣价\",\n" +
                                "\"serviceItems\": [\n" +
                                "\"项目 1\",\n" +
                                "\"项目 2\",\n" +
                                "\"项目 n\"\n" +
                                "]\n" +
                                "},\n" +
                                "\"carOwnerInfo\": {\n" +
                                "\"realName\": \"真实姓名\",\n" +
                                "\"mobile\": \"手机号码\",\n" +
                                "\"carNO\": \"车牌号码\",\n" +
                                "\"carBrand\": \"车辆品牌\",\n" +
                                "\"carFactory\": \"车厂\",\n" +
                                "\"carSerial\": \"车系\",\n" +
                                "\"carModel\": \"车型\"\n" +
                                "},\n" +
                                "\"spInfo\": {\n" +
                                "\"spID\": \"服务商 ID\",\n" +
                                "\"spName\": \"服务商名称\",\n" +
                                "\"carBrand\": \"服务商品牌\"\n" +
                                "}\n" +
                                "}\n" +
                                "}";
                    }
                };
            }
            TypeToken<BaseInfo<OrderInfo>> typeToken =
                    new TypeToken<BaseInfo<OrderInfo>>(){};
            VolleySingleton.getInstance().addToRequestQueue(
                    parameters,callBack,typeToken.getType(),provider);
        }

        @Override
        public void cancelOrder(String sid, String orderNO,
                                ICCallBack<OrderInfo> callBack) {
            BaseParameters parameters = new BaseParameters();
            parameters.add("action","app_CancelOrder");
            parameters.add("sid",sid);
            parameters.add("orderNO",orderNO);
            TypeToken<BaseInfo<OrderInfo>> typeToken =
                    new TypeToken<BaseInfo<OrderInfo>>(){};
            VolleySingleton.getInstance().addToRequestQueue(
                    parameters,callBack,typeToken.getType());
        }
    }
}
